package csu.edu.flatprogram.controller.front;
import csu.edu.flatprogram.common.CommonResponse;
import csu.edu.flatprogram.entity.Category;
import csu.edu.flatprogram.entity.Item;
import csu.edu.flatprogram.entity.Product;
import csu.edu.flatprogram.service.CatalogService;
import csu.edu.flatprogram.service.ItemService;
import csu.edu.flatprogram.service.ProductService;
import csu.edu.flatprogram.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/catalog/")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ItemService itemService;

    @GetMapping("categories")
    @ResponseBody
    public CommonResponse<List<Category>> getCategoryList(){
        return catalogService.getCategoryList();
    }
    @GetMapping("categories/{id}")
    @ResponseBody
    public CommonResponse<Category> getCategory(@PathVariable("id")String categoryId){
        return catalogService.getCategory(categoryId);
    }

    @GetMapping("categories/{id}/products")
    @ResponseBody
    public CommonResponse<List<Product>> getProductByCategoryId(@PathVariable("id")String categoryId){
        return productService.getProductListByCategoryId(categoryId);
    }
    @GetMapping("products/{id}")
    @ResponseBody
    public CommonResponse<Product> getProductById(@PathVariable("id")String productId){
        return productService.getProductById(productId);
    }

    @GetMapping("products/{id}/items")
    @ResponseBody
    public CommonResponse<List<ItemVO>> getItemsByProductId(@PathVariable("id")String productId){
        return itemService.getItemsByProductId(productId);
    }

    @GetMapping("items/{id}")
    @ResponseBody
    public CommonResponse<ItemVO> getItemById(@PathVariable("id") String itemId) {
        return catalogService.getItemById(itemId) ;
    }

    //添加商品item
    @PostMapping("/add")
    @ResponseBody
    public CommonResponse<ItemVO> addOrder(@RequestBody ItemVO itemVO){
        return itemService.addItem(itemVO);
    }

    //删除商品item
    @DeleteMapping("/delete")
    @ResponseBody
    public CommonResponse<ItemVO> deleteOrder(@RequestBody ItemVO itemVO){return itemService.deleteItem(itemVO);}

    //更新商品信息
    //更新信息
    @PutMapping("/update")
    @ResponseBody
    public CommonResponse<String> updateAccount(@RequestBody Item item){
        return itemService.updateItem(item);
    }
}
